using System.Text.RegularExpressions;
using Newtonsoft.Json.Linq;

namespace my_newconsole.Report;

public abstract class Base_Structural
{
    protected HISRISReportDTO ReportDTO { get; set; }

    public Base_Structural(HISRISReportDTO report)
    {
        ReportDTO = report;
    }

    /// <summary>
    /// 获取html中的table元素，并转化为json
    /// </summary>
    /// <returns></returns>
    protected JObject GetData()
    {
        var html = ReportDTO.ReportContent;
        var match = Regex.Match(html, "<table class=\"TechParams\"[\\s\\S]+?</table>");
        var table = match.Value.Replace("&nbsp;", "").Replace("<br>", "");

        // 将input标签，替换为文本
        var matchInput = Regex.Matches(table, "<input.+?>");
        foreach (Match item in matchInput)
        {
            var m = Regex.Match(item.Value, "value=\"(\\S+?)\"");
            var val = string.Empty;
            if (m.Groups.Count == 2)
            {
                val = m.Groups[1].Value;
            }
            table = table.Replace(item.Value, val);
        }

        var json = table.ToJObject();
        return json;
    }

    /// <summary>
    /// 生成结构化报告
    /// </summary>
    /// <returns></returns>
    public virtual ParmDetails Generate()
    {
        var json = GetData();
        var detail = GetDetails(json);
        return detail;
    }

    /// <summary>
    /// 获取结构化报告的指标
    /// </summary>
    /// <returns></returns>
    public abstract List<IndexItem> GetIndexs();

    /// <summary>
    /// 根据指标生成各项指标对应的报告内容
    /// </summary>
    /// <param name="json"></param>
    /// <returns></returns>
    public virtual ParmDetails GetDetails(JObject json)
    {
        var rows = json.SelectToken("table.tbody.tr").ToArray();
        var details = new List<ParmDetail>();
        int num = 0;
        var indexs = GetIndexs();
        foreach (var item in indexs)
        {
            num++;
            // 获取该行所有的单元格
            var columns = rows[item.Row]["td"].ToArray();
            // 找到指定的单元格
            var td = columns[item.Column];
            var val = td.SelectToken("#text")?.Value<string>();
            if (string.IsNullOrEmpty(val))
            {
                var span = td.SelectToken("span");
                val = span?.SelectToken("#text")?.Value<string>();
            }
            if (string.IsNullOrEmpty(val))
            {
                var span = td.SelectToken("div");
                val = span?.SelectToken("#text")?.Value<string>();
            }
            // 根据指标的定义，转化为对应的值
            val = item.Convert(val);
            // 不存在值时，不保存此指标
            if (string.IsNullOrEmpty(val))
                continue;
            details.Add(
                new ParmDetail
                {
                    ParmItemNo = item.Code,
                    ParmEngName = item.EnglishName,
                    ParmChnName = item.Name,
                    ParmValue = val,
                    ParmSortNo = num.ToString(),
                    Remark = ""
                }
            );
        }
        return new ParmDetails { ParmDetail = details };
    }
}
